import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'page_router/custom_page_route.dart';
import '../widget/webview_page.dart';

/// 路由工具类
///
///                    .::::.
///                  .::::::::.
///                 :::::::::::
///             ..:::::::::::'
///           '::::::::::::'
///             .::::::::::
///        '::::::::::::::..
///             ..::::::::::::.
///           ``::::::::::::::::
///            ::::``:::::::::'        .:::.
///           ::::'   ':::::'       .::::::::.
///         .::::'      ::::     .:::::::'::::.
///        .:::'       :::::  .:::::::::' ':::::.
///       .::'        :::::.:::::::::'      ':::::.
///      .::'         ::::::::::::::'         ``::::.
///  ...:::           ::::::::::::'              ``::.
/// ```` ':.          ':::::::::'                  ::::..
///                    '.:::::'                    ':'````..
///
/// Created by Wongxd on 2020/5/19.
/// https://github.com/wongxd
/// wxd1@live.com
///
class RouteUtil {
  ///页面跳转
  static Future<T> _routePagerNavigator<T>(BuildContext context, Widget v,
      {bool isReplace = false, String pageName}) {
//  Navigator.push(context, new MaterialPageRoute(builder: (context) => v));

//    var router = new PageRouteBuilder(
//        opaque: false,
//        pageBuilder: (BuildContext context, _, __) {
//          return v;
//        },
//        transitionsBuilder: (_, Animation<double> animation, __, Widget child) {
//          return new FadeTransition(
//            opacity: animation,
//            child: new FadeTransition(
//              opacity:
//                  new Tween<double>(begin: 0.5, end: 1.0).animate(animation),
//              child: child,
//            ),
//          );
//        });

//    var router = MaterialPageRoute(
//        builder: (context) => v, settings: RouteSettings(name: pageName));

//    var router = RippleRoute(
//      v,
//      RouteConfig.fromContext(context),
//      routeSettings: RouteSettings(name: pageName),
//    );

    var router = CustomRouteSlide(
      v,
      routeSettings: RouteSettings(name: pageName),
    );

    return _routePage(context, router, isReplace: isReplace);
  }

  ///页面跳转
  static Future<T> _routePage<T>(BuildContext context, Route router,
      {bool isReplace = false}) {
    if (isReplace) {
      return Navigator.of(context).pushReplacement(router);
    } else {
      return Navigator.of(context).push(router);
    }
  }

  /// 跳转页面
  static Future<T> push<T>(BuildContext context, Widget page,
      {bool isReplace = false, String pageName}) async {
    if (context == null || page == null) return null;
//    final result = await Navigator.push(
//        context, MaterialPageRoute(builder: (context) => page));

    final result = await _routePagerNavigator(context, page,
        isReplace: isReplace, pageName: pageName);

    return result;
  }

  /// 跳转到 WebView 打开
  static void toWebView(BuildContext context, String title, String url) async {
    if (context == null || url.isEmpty) return;
    if (url.endsWith('.apk')) {
      launchInBrowser(url, title: title);
    } else {
      await Navigator.of(context)
          .push(new CupertinoPageRoute<void>(builder: (context) {
        return new WebViewPage(
          title: title,
          url: url,
        );
      }));
    }
  }

  static Future<Null> launchInBrowser(String url, {String title}) async {
    if (await canLaunch(url)) {
      await launch(url, forceSafariVC: false, forceWebView: false);
    } else {
      throw 'Could not launch $url';
    }
  }
}
